﻿using System;
using System.Windows.Forms;
using Me.Amon.POS.M;
using Me.Amon.Properties;
using Me.Amon.Util;
using Newtonsoft.Json;

namespace Me.Amon.POS.V
{
    public partial class WrSpu : Form
    {
        private Settings _Settings;
        private MSpu _MSpu;
        public WrSpu()
        {
            _Settings = Settings.Default;
            InitializeComponent();
        }

        private void WrSpu_Load(object sender, EventArgs e)
        {
            TbName.SelectAll();
            TbCount.SelectAll();
            TbUnit.SelectAll();
            TbBarcode.SelectAll();
        }

        public void ShowPay()
        {
            TbName.Focus();
        }

        private void WrSpu_KeyDown(object sender, KeyEventArgs e)
        {
           if (e.KeyCode == Keys.Escape)
            {
                e.Handled = true;
                Close();
                return;
            }
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            var name = TbName.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBox.Show("商品名称不能为空！");
                TbName.Focus();
                return;
            }

            var count = TbCount.Text.Trim();
            if (string.IsNullOrWhiteSpace(count))
            {
                MessageBox.Show("商品数量不能为空！");
                TbCount.Focus();
                return;
            }

            var unit = TbUnit.Text.Trim();
            if (string.IsNullOrWhiteSpace(unit))
            {
                MessageBox.Show("计量单位不能为空！");
                TbUnit.Focus();
                return;
            }

            var barcode = TbBarcode.Text.Trim();
            if (string.IsNullOrWhiteSpace(barcode))
            {
                MessageBox.Show("条形码不能为空！");
                TbBarcode.Focus();
                return;
            }

            _MSpu = new MSpu();
            _MSpu.barcodes = new MSpuBarcode[1];
            MSpuBarcode bar = new MSpuBarcode();

            _MSpu.name = name;
            _MSpu.usable_inv = Int32.Parse(count);
            _MSpu.uom_id = unit;
            bar.barcode = barcode;
            _MSpu.barcodes[0] = bar;
            _MSpu.corp_id = _Settings.CorpCode;

            var add = string.Format("{0}/POS/Spu.ashx?{1}={2}&{3}={4}", _Settings.Server, PPos.PARAM_KEY_CORP, _Settings.CorpCode, PPos.PARAM_KEY_OPT, PPos.PARAM_VALUE_OPT_CREATE);
            var rest = WebUtil.POST(add, string.Format("{0}={1}", PPos.PARAM_KEY_DATA, JsonConvert.SerializeObject(_MSpu)));
            if (string.IsNullOrWhiteSpace(rest))
            {
                MessageBox.Show("系统异常，请与管理员联系！");
                return;
            }
            var obj = JsonConvert.DeserializeObject<MSpu>(rest);
            if (obj.HasError())
            {
                MessageBox.Show(obj.GetError());
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
